package cn.lblbc.webbrowser.utils

import android.content.Context
import android.content.SharedPreferences

class ImageCollectionSettings(context: Context) {
    
    private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
    
    companion object {
        private const val PREFS_NAME = "image_collection_settings"
        private const val KEY_ENABLED = "image_collection_enabled"
        private const val KEY_MIN_WIDTH = "min_image_width"
        private const val KEY_MIN_HEIGHT = "min_image_height"
        
        // 预设分辨率选项
        const val RESOLUTION_ANY = 0
        const val RESOLUTION_SMALL = 1  // 100x100
        const val RESOLUTION_MEDIUM = 2 // 300x300
        const val RESOLUTION_LARGE = 3  // 500x500
        const val RESOLUTION_XLARGE = 4 // 800x800
        const val RESOLUTION_CUSTOM = 5
    }
    
    // 是否启用图片收集
    var isEnabled: Boolean
        get() = prefs.getBoolean(KEY_ENABLED, true)
        set(value) = prefs.edit().putBoolean(KEY_ENABLED, value).apply()
    
    // 最小宽度
    var minWidth: Int
        get() = prefs.getInt(KEY_MIN_WIDTH, 800)
        set(value) = prefs.edit().putInt(KEY_MIN_WIDTH, value).apply()
    
    // 最小高度
    var minHeight: Int
        get() = prefs.getInt(KEY_MIN_HEIGHT, 800)
        set(value) = prefs.edit().putInt(KEY_MIN_HEIGHT, value).apply()
    
    // 设置预设分辨率
    fun setPresetResolution(preset: Int) {
        when (preset) {
            RESOLUTION_ANY -> {
                minWidth = 0
                minHeight = 0
            }
            RESOLUTION_SMALL -> {
                minWidth = 100
                minHeight = 100
            }
            RESOLUTION_MEDIUM -> {
                minWidth = 300
                minHeight = 300
            }
            RESOLUTION_LARGE -> {
                minWidth = 500
                minHeight = 500
            }
            RESOLUTION_XLARGE -> {
                minWidth = 800
                minHeight = 800
            }
        }
    }
    
    // 获取当前分辨率对应的预设类型
    fun getCurrentPreset(): Int {
        return when {
            minWidth == 0 && minHeight == 0 -> RESOLUTION_ANY
            minWidth == 100 && minHeight == 100 -> RESOLUTION_SMALL
            minWidth == 300 && minHeight == 300 -> RESOLUTION_MEDIUM
            minWidth == 500 && minHeight == 500 -> RESOLUTION_LARGE
            minWidth == 800 && minHeight == 800 -> RESOLUTION_XLARGE
            else -> RESOLUTION_CUSTOM
        }
    }
    
    // 检查图片是否符合收集条件
    fun shouldCollectImage(width: Int, height: Int): Boolean {
        if (!isEnabled) return false
        return width >= minWidth && height >= minHeight
    }
    
    // 获取分辨率描述文字
    fun getResolutionDescription(context: Context): String {
        return when (getCurrentPreset()) {
            RESOLUTION_ANY -> context.getString(cn.lblbc.webbrowser.R.string.resolution_any)
            RESOLUTION_SMALL -> context.getString(cn.lblbc.webbrowser.R.string.resolution_small)
            RESOLUTION_MEDIUM -> context.getString(cn.lblbc.webbrowser.R.string.resolution_medium)
            RESOLUTION_LARGE -> context.getString(cn.lblbc.webbrowser.R.string.resolution_large)
            RESOLUTION_XLARGE -> context.getString(cn.lblbc.webbrowser.R.string.resolution_xlarge)
            else -> context.getString(cn.lblbc.webbrowser.R.string.resolution_format, minWidth, minHeight)
        }
    }
}